package stack;

public class MyStack {
    String[] arr;
    int size;

    public MyStack() {
        arr = new String[100];
        size = 0;
    }

    public MyStack(int data) {
        arr = new String[data];
        size = 0;
    }

    //入栈
    public void push(String data) {
        if (size == arr.length) {
            resize();
        }
        arr[size] = data;
        size++;
    }

    //出栈
    public String pop() {
        if (size == 0) {
            // 抛出异常
            throw new RuntimeException("Stack is empty!");
        }
        size--;
        return arr[size];
    }

    //查看栈顶元素
    public String peek() {
        if (size == 0) {
            // 抛出异常
            throw new RuntimeException("Stack is empty!");
        }
        return arr[size - 1];
    }

    //扩容
    public void resize(){
        String[] newarr = new String[arr.length + arr.length >> 1];
        for (int i = 0;i < size;i++) {
            newarr[i] = arr[i];
        }
        arr = newarr;
    }

    //判断栈是否为空
    public boolean empty() {
        if (size == 0) {
            return true;
        }
        return false;
    }
}
